﻿using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.Plugin;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;

namespace CollectionOperationKit
{
    [Icon("pack://application:,,,/CollectionOperationKit;component/Resources/ObjectIcon.png")]
    [Category("对象与集合操作")]
    public class ServerSideDataConvert : BaseServerCommand, ICommandExecutableInServerSide
    {

        /// <summary>
        /// 在设计器中展示的插件名称
        /// </summary>
        /// <returns>易读的字符串</returns>
        public override string ToString()
        {

            if (String.IsNullOrEmpty(OutParamaterName))
            {
                return "数据类型转换";
            }
            else
            {
                return "转换为" + this.OperationParamaterValueType + "：" + OutParamaterName;
            }



        }

        public ExecuteResult Execute(IServerCommandExecuteContext dataContext)
        {
            var input = getParamValue(dataContext, this.InParamater, false);
            var defaultV = getParamValue(dataContext, this.DefaultParamater, false, OperationParamaterValueType);
            var nullV = getParamValue(dataContext, this.NullValueParamater, false, OperationParamaterValueType);

            if (input == null) {
                returnToParam(dataContext, ServerSideHelpers.ConvertData(nullV, OperationParamaterValueType, defaultV));
            } else {
                returnToParam(dataContext, ServerSideHelpers.ConvertData(input, OperationParamaterValueType, defaultV));
            }

            return new ExecuteResult();

        }


        [OrderWeight(10)]
        [DisplayName("原始数据（输入参数）")]
        [FormulaProperty]
        [Description("操作可能不会影响【输入参数】中用到的变量，如需对变量进行修改，可将其填写到【将结果返回到变量】。")]
        public object InParamater { get; set; }

        [OrderWeight(103)]
        [DisplayName("目标类型")]
        [ComboProperty]
        public PropertyDataType OperationParamaterValueType { get; set; } = PropertyDataType.AUTO;

        [OrderWeight(110)]
        [DisplayName("默认值")]
        [FormulaProperty]
        [Description("如果输入参数不能转换为该数据类型时，将返回【默认值】。")]
        public object DefaultParamater { get; set; }

        [OrderWeight(105)]
        [DisplayName("Null值")]
        [FormulaProperty]
        [Description("如果输入参数为Null时，将返回【Null值】。")]
        public object NullValueParamater { get; set; }
    }
}
